package com.cpi.cornpp.common.files;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.common.files.dto.SAPComponentCornCostDTO;
import com.cpi.cornpp.common.files.dto.SAPComponentDTO;
import com.cpi.cornpp.common.files.exception.SAPComponentFileException;
import com.cs.common.pool.exception.PoolException;
import com.cs.common.utils.NumberUtil;

public class SAPComponentCornCost extends SAPComponentFile {
	private static final String CHAR_DELIMITER = "|";//$NON-NLS-1$
	private static final String SQL_INSERT_CORN_COST = "INSERT INTO sap_corn_cost ([year] ,[month] ,[plant] ,[raw_material] ,[currency] ,[value]) VALUES (?, ?, ?, ?, ?, ?)";//$NON-NLS-1$
	private static final int OFF_SET_INIT = 3;
	public SAPComponentCornCost(String fileName) {
		super(fileName);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#parse(java.util.List)
	 */
	public List parse(List list) throws SAPComponentFileException {
		List sapComponentCornCostDTOs = null;
		Iterator iterator = null;
		String line = null;
		SAPComponentCornCostDTO sapComponentCornCostDTO = null;
		StringTokenizer stringTokenizer = null;
		String token = null;
		int totalReg = 0;
		try{
			iterator = list.iterator();
			totalReg = list.size();
			sapComponentCornCostDTOs = new ArrayList(0);
			for( int mainIndex = 0; mainIndex < totalReg ; mainIndex++ ){
				line = (String)iterator.next();
				if( mainIndex+1 > OFF_SET_INIT ){
					stringTokenizer = new StringTokenizer(line, CHAR_DELIMITER, false);
					sapComponentCornCostDTO = new SAPComponentCornCostDTO();
					for(int index = 0;stringTokenizer.hasMoreTokens();index++){
						token = stringTokenizer.nextToken();
						switch(index){
							case 0:	sapComponentCornCostDTO.setYear(NumberUtil.parseInt(token));
										break;
							case 1:	sapComponentCornCostDTO.setMonth(NumberUtil.parseInt(token));
										break;
							case 2: sapComponentCornCostDTO.setPlant(token);
										break;
							case 3:	sapComponentCornCostDTO.setRawMaterial(token);
										break;
							case 4 :	sapComponentCornCostDTO.setCurrency(token);
										break;
							case 5:	sapComponentCornCostDTO.setAmount(NumberUtil.parseDouble(token));
										break;
						}
					}
					sapComponentCornCostDTOs.add(sapComponentCornCostDTO);
				}
			}
		}catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			sapComponentFileException = new SAPComponentFileException(exception, CornPPException.LAYER_SERVICE, CornPPException.ACTION_LISTS);
			throw sapComponentFileException;
		}
		return sapComponentCornCostDTOs;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insertAll(java.util.List)
	 */
	public int insertAll(List list)throws SAPComponentFileException{
		int totalInserted = 0;
		Iterator iterator = null;
		SAPComponentCornCostDTO sapComponentCornCostDTO;
		try{
			iterator = list.iterator();
			while(iterator.hasNext()){
				sapComponentCornCostDTO = (SAPComponentCornCostDTO)iterator.next();
				try{
					insert(sapComponentCornCostDTO);
					totalInserted++;
				}catch(SAPComponentFileException sapComponentFileException){
					System.out.println("Error DTO ["+sapComponentFileException.getMessage()+"] : " + sapComponentCornCostDTO);
				}
			}
		} catch(Exception exception){
			SAPComponentFileException sapComponentFileException = null;
			exception.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (exception, CornPPException.LAYER_DAO,CornPPException.ACTION_SELECT);
			throw sapComponentFileException;
		}
		return totalInserted;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#insert(com.cpi.cornpp.common.files.dto.SAPComponentDTO)
	 */
	public void insert(SAPComponentDTO sapComponentDTO)throws SAPComponentFileException{
		SAPComponentCornCostDTO sapComponentCornCostDTO = null;
		PreparedStatement preparedStatement = null;
		try{
			sapComponentCornCostDTO = (SAPComponentCornCostDTO)sapComponentDTO;
			preparedStatement = prepareSQL(SQL_INSERT_CORN_COST, dsName);
			preparedStatement.setInt(1, sapComponentCornCostDTO.getYear());
			preparedStatement.setInt(2, sapComponentCornCostDTO.getMonth());
			preparedStatement.setString(3, sapComponentCornCostDTO.getPlant());
			preparedStatement.setString(4, sapComponentCornCostDTO.getRawMaterial());
			preparedStatement.setString(5, sapComponentCornCostDTO.getCurrency());
			preparedStatement.setDouble(6, sapComponentCornCostDTO.getAmount());
			executeInsert(preparedStatement);
		}catch(PoolException poolException){
			SAPComponentFileException sapComponentFileException = null;
			poolException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}catch(SQLException sqlException){
			SAPComponentFileException sapComponentFileException = null;
			sqlException.printStackTrace(System.out);
			sapComponentFileException = new SAPComponentFileException (sqlException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw sapComponentFileException;
		}finally{
			try {
				closeConnection(preparedStatement.getConnection());
			} catch (SQLException sqlException) {}
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.cpi.cornpp.common.files.SAPComponentFile#size()
	 */
	public int size() throws SAPComponentFileException {
		int size = 0;
		List records = null;
		try{
			records = getContent();
			size = records.size() - OFF_SET_INIT;
		}finally{
			records = null;
		}
		return size;
	}
}